package com.qianwang.shopmsg.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.qianwang.shopmsg.common.AjaxResult;
import com.qianwang.shopmsg.common.Page;
import com.qianwang.shopmsg.common.PageData;
import com.qianwang.shopmsg.entity.StudentSign;
import com.qianwang.shopmsg.entity.view.StudentSignView;
import com.qianwang.shopmsg.exception.ParamValidateException;
import com.qianwang.shopmsg.param.StudentSignCreateParam;
import com.qianwang.shopmsg.service.sign.StudentMsgService;

@Controller
@RequestMapping("studentMsg")
@SuppressWarnings("all")
public class StudentMsgController extends BaseController{

	private final static Logger logger =  LoggerFactory.getLogger(StudentMsgController.class);
	
	@Autowired
	private StudentMsgService studentMsgService;
	
	
	@RequestMapping(value="showStudentSignInfos")
	public ModelAndView showStudentSignInfos(){
		ModelAndView mav = new ModelAndView("studentMsg/studentSignInfos");
		return mav;
	}
	
	@RequestMapping(value="listStudentSignInfos")
	@ResponseBody
	public PageData<StudentSign> listStudentSignInfos(@RequestParam("rows") int rows,@RequestParam("page") int page,
			@RequestParam(value="startTime",required=false) String startTime,@RequestParam(value="endTime",required=false) String endTime,
			@RequestParam(value="studentName",required=false) String studentName){
		PageData<StudentSign> data = null;
		try{
			Page pg = new Page(rows, page);
			data = new PageData<StudentSign>();
			data.setTotal(studentMsgService.countStudentSingInfos(startTime,endTime,studentName));
			data.setRows(studentMsgService.listStudentSignInfos(pg,startTime,endTime,studentName));
		}catch(Exception e){
			logger.error(e.getMessage(),e);
		}
		return data;
	}
	
	@RequestMapping(value = "/saveStudentSign", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult saveStudentSign(@RequestParam("paramJson") String paramJson) {
		try{
			StudentSignCreateParam signParam = JSONObject.parseObject(paramJson, StudentSignCreateParam.class);
			studentMsgService.saveStudentSign(signParam);
			return AjaxResult.success(null,"恭喜，操作成功");
		}catch(ParamValidateException e){
			logger.error(e.getMessage(),e);
			return AjaxResult.failed(e.getMessage());
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			return AjaxResult.failed("操作失败，请稍后重试");
		}
	}
	
	@RequestMapping(value = "/deleteStudentSign", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult deleteStudentSign(@RequestParam("id") long id) {
		studentMsgService.deleteStudentSign(id);
		return AjaxResult.success("操作成功");
	}
	
	@RequestMapping(value = "/loadStudentSign", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult loadStudentSign(@RequestParam("id") long id) {
		try {
			StudentSignView view = studentMsgService.loadSignWithStuInfoById(id);
			return AjaxResult.success(view);
		} catch (ParamValidateException e) {
			logger.error(e.getMessage(),e);
			return AjaxResult.failed(e.getMessage());
		}
	}
	
}
